<?php 
session_start();
include "../_init_.php";


$dtRent = new DataTable(constant('DB_TABLE_RENT'));
//增加数据
$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'add':
		$update_data = array(
			'id'=>$_REQUEST['id'],
			'show_on_front'=>'Y',
			'front_sn'=>0
		);
		$dtRent->save($update_data);
	break;
	case 'delete':
		//delete image
		$update_data = array(
			'id'=>$_REQUEST['id'],
			'show_on_front'=>'N'
		);
		$dtRent->save($update_data);
	break;
	case 'moveup':
		$rowData = $dtRent->get($_REQUEST['id']);
		$query = sprintf("SELECT `front_sn` FROM %s WHERE `show_on_front`='Y' AND `front_sn` < %d ORDER BY `front_sn` DESC LIMIT 0,1",constant('DB_TABLE_RENT'),$rowData['front_sn']);
		$result = mysql_query($query);
		if($result === false)
		{
			echo mysql_error().__LINE__;
		}
		$update_data = array('id'=>$rowData['id']);
		if($row = mysql_fetch_assoc($result))
		{
			$update_data['front_sn'] = $row['front_sn'] - 1;
		}else{
			$update_data['front_sn'] = $rowData['front_sn'] - 1;
		}
		$dtRent->save($update_data);
		
	break;
	case 'movedown':
		$rowData = $dtRent->get($_REQUEST['id']);
		$query = sprintf("SELECT `front_sn` FROM %s WHERE `show_on_front`='Y' AND `front_sn` > %d ORDER BY `front_sn` ASC LIMIT 0,1",constant('DB_TABLE_RENT'),$rowData['front_sn']);
		$result = mysql_query($query);
		if($result === false)
		{
			echo mysql_error().__LINE__;
		}
		$update_data = array('id'=>$rowData['id']);
		
		if($row = mysql_fetch_assoc($result))
		{
			echo  $row['front_sn'];
			$update_data['front_sn'] = $row['front_sn'] + 1;
		}else{
			$update_data['front_sn'] = $rowData['front_sn'] + 1;
		}
		
		$dtRent->save($update_data);
	break;
}

$condition = "`show_on_front`='Y' ";
$orderby = 'front_sn';
$data_list = $dtRent->get_list($condition,$orderby);
?>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>

<script src="../js/jquery-ui-1.7.3.custom/js/jquery-1.3.2.min.js" language="JavaScript" type="text/javascript"></script>
<script src="../js/jquery.pager.js"></script>
<link href="../css/page.css" rel="stylesheet" type="text/css" /> 
<script language="javascript">
$(function(){
/*
	$(".pager").pager({ pagenumber: <?php echo $pagenumber;?>, pagecount: <?php echo $pagecount;?>, buttonClickCallback: function(pageclickednumber){
			<?php if(isset($_REQUEST['sText']) && strlen($_REQUEST['sText']) > 0):?>
			document.location='?sText=<?php echo urlencode($_REQUEST['sText']);?>&pagenumber='+pageclickednumber;
			<?php else:?>
			document.location='?pagenumber='+pageclickednumber;
			<?php endif;?>
			
     } }); */
});
function ConfirmDel()
{
   if(confirm("確定要刪除首頁最新房屋?"))
     return true;
   else
     return false;	 
}
</script>
<body onLoad="setupZoom();">
<table width="98%" border="0" class="table2">
  <tr>
    <td>首頁管理 - 首頁最新房屋</td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
 <table width="820" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
 		<tr>
 			<td height="20"><input type="button" value="增加" onclick="document.location = 'admin_index_rent_picker.php';"/></td>
 		</tr>
        <tr> 
          <td  height="20" class="back_southidc"> <div align="center"><strong>首頁最新房屋</strong></div></td>
        </tr>
        <tr  class="tr_southidc">          
            <td bgcolor="#B0DAFF">
              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="2" bordercolor="#0000FF">
                <tr bgcolor="#E4F2FF"> 
                  <td width="10%" height="25"  bgcolor="#D9EDFF"> 
                  	<div align="center">標題</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">類型</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">坪數</div>
                  </td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">租金</div>
                  </td>
                	<td width="10%" bgcolor="#D9EDFF"> 
                  	<div align="center">圖片</div>
                  </td>
                  <td width="8%" bgcolor="#D9EDFF"> 
                  	<div align="center">操作</div>
                  </td>
                  <td width="5%" bgcolor="#D9EDFF"> 
                  	<div align="center">不顯示</div>
                  </td>
                </tr>
<?php 	
	foreach($data_list as $row):
	?>
 <tr bgcolor="#FFFFFF" onMouseOver="this.style.backgroundColor='#E4F2FF'" onMouseOut="this.style.backgroundColor=''"> 
                  <td height="22"> 
                  <div align="center"><?php echo $row['title'];?></div></td>
                  <td> 
                  <div align="center"><?php echo $row['type'];?></div>
                  </td>
                     <td> 
                  <div align="center"><?php echo $row['size'];?></div>
                  </td>
                	<td> 
                  <div align="center"><?php echo $row['cost'];?></div>
                  </td>
                  	<td> 
                  <div align="center"><image src="<?php echo constant('SITE_URL_PATH').$row['image'];?>" width="60" height="60"/></div>
                  </td>
                  <td> 
                    <div align="center">
                  <a href='?action=moveup&id=<?php echo $row['id'];?>'>上移</a>&nbsp;&nbsp;  
                   <a href='?action=movedown&id=<?php echo $row['id'];?>'>下移</a>
                  </div></td>
                  <td> 
                    <div align="center">
				  <a href="?id=<?php echo $row['id'];?>&action=delete" onClick="return ConfirmDel();">不顯示</a>                    </div></td>
                </tr>
<?php endforeach;?>
          </table></td>         
        </tr>
      </table>
      <div class="pager"></div>
</body>
</html>